<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据号" prop="vbillcode">
        <el-input
          v-model="queryParams.vbillcode"
          placeholder="请输入单据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据日期" prop="dbizdate">
        <el-date-picker clearable
          v-model="queryParams.dbizdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择单据日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="单据日期">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeDbizdate"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="仓库" prop="ckid">
<!--        <el-input-->
<!--          v-model="queryParams.ckid"-->
<!--          placeholder="请输入仓库id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.ckid" placeholder="仓库" clearable>
          <el-option
            v-for="dict in dict.type.warehouse"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库管员" prop="kgyid">
<!--        <el-input-->
<!--          v-model="queryParams.kgyid"-->
<!--          placeholder="请输入库管员ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.kgyid" placeholder="库管员" clearable>
          <el-option
            v-for="dict in userList"
            :key="dict.userId"
            :label="dict.userName"
            :value="dict.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物资编码" prop="invcode">
        <el-input
          v-model="queryParams.invcode"
          placeholder="请输入物资编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资名称" prop="invname">
        <el-input
          v-model="queryParams.invname"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ic:ic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ic:ic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ic:ic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ic:ic:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="icList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="单据号" align="center" prop="vbillcode" />
      <el-table-column label="单据日期" align="center" prop="dbizdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dbizdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="ckid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warehouse" :value="scope.row.ckid"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="库管员ID" align="center" prop="kgyid" />-->
      <el-table-column label="库管员" align="center" prop="kgyname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="creater" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="更新时间" align="center" prop="updatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="删除状态" align="center" prop="del" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="details(scope.row)"
            v-hasPermi="['ic:ic:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ic:ic:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ic:ic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出库单头，用于保存出库单主要信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据日期" prop="dbizdate">
          <el-date-picker clearable
            v-model="form.dbizdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="仓库" prop="ckid">
<!--          <el-input v-model="form.ckid" placeholder="请输入仓库id" />-->
          <el-select v-model="form.ckid" placeholder="仓库" clearable>
            <el-option
              v-for="dict in dict.type.warehouse"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="库管员" prop="kgyid">
<!--          <el-input v-model="form.kgyid" placeholder="请输入库管员ID" />-->
          <el-select v-model="form.kgyid" placeholder="库管员" clearable>
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.userName"
              :value="dict.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

        <el-divider content-position="center">出库单明细</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddIcGeneralB">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteIcGeneralB">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="icGeneralBList" :row-class-name="rowIcGeneralBIndex" @selection-change="handleIcGeneralBSelectionChange" ref="icGeneralB">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物资编码" prop="invcode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.invcode" placeholder="请输入物资编码" />
            </template>
          </el-table-column>
          <el-table-column label="物资名称" prop="invname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.invname" placeholder="请输入物资名称" />
            </template>
          </el-table-column>
          <el-table-column label="出库数量" prop="nnumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nnumber" placeholder="请输入出库数量" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    详情-->
    <el-dialog :title="title" :visible.sync="openDetails" width="1000px" append-to-body>
      <el-table v-loading="loading" :data="detailsData" @selection-change="handleSelectionChange">
        <el-table-column label="出库单表头id" align="center" prop="hid" />
        <el-table-column label="物资编码" align="center" prop="invcode" />
        <el-table-column label="物资名称" align="center" prop="invname" />
        <el-table-column label="出库数量" align="center" prop="nnumber" />
        <el-table-column label="创建人" align="center" prop="creater" />
        <el-table-column label="创建时间" align="center" prop="createtime" />
        <el-table-column label="更新人" align="center" prop="updater" />
        <el-table-column label="更新时间" align="center" prop="updatetime" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listIc, getIc, delIc, addIc, updateIc } from "@/api/ic/ic";
import { listUser } from "@/api/system/user";

export default {
  name: "Ic",
  dicts: ['warehouse'],
  data() {
    return {
      userList: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedIcGeneralB: [],
      // 删除状态时间范围
      daterangeDbizdate: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单头，用于保存出库单主要信息表格数据
      icList: [],
      // 出库单明细，用于保存物资明细信息表格数据
      icGeneralBList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetails:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vbillcode: null,
        dbizdate: null,
        ckid: null,
        kgyid: null,
        invcode: null,
        invname: null,
        nnumber:null,
      },
      // 表单参数
      form: {},
      // 详细数据
      detailsData: {},
      // 表单校验
      rules: {
        vbillcode: [
          { required: true, message: "单据号不能为空", trigger: "blur" }
        ],
        dbizdate: [
          { required: true, message: "单据日期不能为空", trigger: "blur" }
        ],
        ckid: [
          { required: true, message: "仓库id不能为空", trigger: "blur" }
        ],
        kgyid: [
          { required: true, message: "库管员ID不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseKeeper();
  },
  methods: {
    /*获取库管员*/
    getWarehouseKeeper(){
      listUser(null).then(res=>{
        this.userList = res.rows
      })
    },

    /** 查询出库单头，用于保存出库单主要信息列表 */
    getList() {
      this.loading = true;
      listIc(this.queryParams).then(response => {
        console.log(response)
        this.icList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vbillcode: null,
        dbizdate: null,
        ckid: null,
        kgyid: null,
        remark: null,
        creater: null,
        createtime: null,
        updater: null,
        updatetime: null,
        del: null
      };
      this.icGeneralBList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库单头，用于保存出库单主要信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIc(id).then(response => {
        this.form = response.data;
        this.icGeneralBList = response.data.icGeneralBList;
        this.open = true;
        this.title = "修改出库单头，用于保存出库单主要信息";
      });
    },
    /*详情按钮*/
    details(row){
      this.reset();
      const id = row.id || this.ids
      getIc(id).then(response => {
        this.form = response.data;
        this.detailsData = response.data.icGeneralBList;
        console.log(this.detailsData)
        this.openDetails = true;
        this.title = "详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.icGeneralBList = this.icGeneralBList;
          if (this.form.id != null) {
            updateIc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库单头，用于保存出库单主要信息编号为"' + ids + '"的数据项？删除出库单后，将清空所有出库明细。\n').then(function() {
        return delIc(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 出库单明细，用于保存物资明细信息序号 */
    rowIcGeneralBIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 出库单明细，用于保存物资明细信息添加按钮操作 */
    handleAddIcGeneralB() {
      let obj = {};
      obj.invcode = "";
      obj.invname = "";
      obj.nnumber = "";
      obj.creater = "";
      obj.createtime = "";
      obj.updater = "";
      obj.updatetime = "";
      obj.del = "";
      this.icGeneralBList.push(obj);
    },
    /** 出库单明细，用于保存物资明细信息删除按钮操作 */
    handleDeleteIcGeneralB() {
      if (this.checkedIcGeneralB.length == 0) {
        this.$modal.msgError("请先选择要删除的出库单明细，用于保存物资明细信息数据");
      } else {
        const icGeneralBList = this.icGeneralBList;
        const checkedIcGeneralB = this.checkedIcGeneralB;
        this.icGeneralBList = icGeneralBList.filter(function(item) {
          return checkedIcGeneralB.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleIcGeneralBSelectionChange(selection) {
      this.checkedIcGeneralB = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ic/ic/export', {
        ...this.queryParams
      }, `ic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
